package fr.irit.etracetool.eobjectgraph;

import java.util.HashSet;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class BipartiteGraphTest {
	
	private BipartiteGraph graph;

	@Before
	public void setUp() throws Exception {
		graph = new BipartiteGraph();
	}
	
	@Test
	public void getSourcesTest() {
		assertNotNull(graph.getSources());
		assertNotNull(graph.getTargets());
	}

	@Test
	public void contains() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");
		Node n1 = new Node(attrA);
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		Node n2 = new Node(attrB);
		HashSet<Node> set = new HashSet<Node>();
		set.add(n1);
		set.add(n2);
		assertTrue(BipartiteGraph.contains(set, attrA));
		assertTrue(BipartiteGraph.contains(set, attrB));
	}
	
	@Test
	public void addInSourcesTest() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		graph.addInSources(attrA);
		graph.addInSources(attrB);
		graph.addInSources(attrA);
		assertTrue(BipartiteGraph.contains(graph.getSources(), attrA));
		assertTrue(BipartiteGraph.contains(graph.getSources(), attrB));
		assertEquals(graph.getSources().size(), 2);
	}

	@Test
	public void addInTargetsTest() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		graph.addInTargets(attrA);
		graph.addInTargets(attrB);
		graph.addInTargets(attrA);
		assertTrue(BipartiteGraph.contains(graph.getTargets(), attrA));
		assertTrue(BipartiteGraph.contains(graph.getTargets(), attrB));
		assertEquals(graph.getTargets().size(), 2);
	}
	
	@Test
	public void getInSourcesTest() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");	
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		graph.addInSources(attrA);
		Node n = graph.getInSources(attrA);
		assertTrue(EcoreUtil.equals(n.getElement(), attrA));
		assertNull(graph.getInSources(attrB));
	}
	
	@Test
	public void getInTargetsTest() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");	
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		graph.addInTargets(attrA);
		Node n = graph.getInTargets(attrA);
		assertTrue(EcoreUtil.equals(n.getElement(), attrA));
		assertNull(graph.getInTargets(attrB));
	}
	
	@Test
	public void addTest() {
		EAttribute attrA = EcoreFactory.eINSTANCE.createEAttribute();
		attrA.setName("A");	
		EAttribute attrB = EcoreFactory.eINSTANCE.createEAttribute();
		attrB.setName("B");
		graph.add(attrA, attrB);
		assertTrue(BipartiteGraph.contains(graph.getSources(), attrA));
		assertTrue(BipartiteGraph.contains(graph.getTargets(), attrB));
		assertTrue(BipartiteGraph.contains(graph.getInSources(attrA).getSuccessor(), attrB));
		assertTrue(BipartiteGraph.contains(graph.getInTargets(attrB).getSuccessor(), attrA));
		graph.add(attrA, attrB);
		assertTrue(graph.getInSources(attrA).getSuccessor().size() == 1);
	}
	
	@After
	public void tearDown() throws Exception {
	}

}
